<?php
namespace aes;
/**
 * 一个AES算法的加密类
 *
 * @example
 *	 	$aes = new AES();
 *		$aes->setKey(AUTH_KEY);
 *      $aes->setOpenMode(256);
 *		$ret = $aes->encrypt($string);
 *		$string = $aes->decrypt($ret);
 */

class Aes{

    protected $secretKey = 'bam';
    protected $iv = 'abcd';
    protected $openMode = 'AES-128-CBC';

    public function setKey($key)
    {
        $this->secretKey = $key;
        $this->iv = md5($key);
    }

    public function setOpenMode($bit){
        if($bit==256){
            $this->openMode =  'AES-256-CBC';
        }
    }

    public function encrypt($str) {
        return @openssl_encrypt($str, $this->openMode, $this->secretKey, 0, $this->iv);
    }

    public function decrypt($str) {
        return @openssl_decrypt($str, $this->openMode, $this->secretKey, 0 ,$this->iv);
    }

}